package com.lzs.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lzs.entity.Notice;
import com.lzs.system.service.NoticeService;
import com.lzs.util.JwtUtils;
import com.lzs.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author ymw
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/system/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @ApiOperation(value = "通知分页查询")
    @PostMapping("/selectNoticePage/{page}/{limit}")
    public IPage<Notice> selectNoticePage(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Notice notice){
        IPage<Notice> noticePage = noticeService.selectNoticePage(page, limit, notice);
        return noticePage;
    }


    // 新增修改
    @PostMapping("/insertOrUpdateNotice")
    @ApiOperation(value = "通知新增修改")
    public R insertOrUpdateNotice(@RequestBody Notice notice, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getMemberIdByJwtToken(token);
        return noticeService.insertOrUpdateNotice(notice,userId);
    }
    // 修改时回显内容
    @GetMapping("/findByNoticeId/{noticeId}")
    @ApiOperation(value = "修改时回显内容")
    public R findByNoticeId(@PathVariable Long noticeId){
        return noticeService.findByNoticeId(noticeId);
    }

    // 根据Id删除
    @DeleteMapping("/deleteNotice/{noticeId}")
    @ApiOperation(value = "根据通知Id删除")
    public R deleteNotice(@PathVariable Long noticeId){
        boolean b = noticeService.removeById(noticeId);
        if (b){
            return R.ok().message("删除成功");
        }
        return R.ok().message("删除失败");
    }
}

